<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class ListController extends Controller{
//	商品表
	private $goodsModel;
//	分类表
	private $cateModel;
//	商品属性表
	private $gattrModel;
//	类型属性表
	private $attrModel;
//	品牌表
	private $brandModel;
	//构造函数
	public function __init()
	{
		$this->goodsModel = new \Common\Model\Goods;
		$this->cateModel = new \Common\Model\Category;
		$this->gattrModel = new \Common\Model\Gattr;
		$this->attrModel = new \Common\Model\Attr;
		$this->brandModel = new \Common\Model\Brand;
	}
	
    //列表展示
    public function index(){
//  	获得分类id
		$cid = Q('get.cid',0,'intval');
		$bid = Q('get.bid',0,'intval');
		View::with('cid',$cid);
//		获得所有子集id
		$cids = $this->cateModel->getSon($this->cateModel->get(),$cid);
		if($cid!=0){
			$cids[]=$cid;
		}
//		获得所有父集栏目
		$pData = $this->cateModel->getParName($cid);
		View::with('pData',$pData);
//		找出分类与子分类
		$cateData = $this->cateModel->getSonName($cid);
		View::with('cData',$cateData);
		if($bid&&!$cid){
			$gids = $this->goodsModel->where("brand_bid = $bid")->lists('gid');
		}
//		获取商品属性，分配到页面循环用于筛选
//		1.根据当前的cid查找商品对应gid
		if($cids&&!$bid){
			$gids = $this->goodsModel->where("category_cid in (".implode(',', $cids).")")->lists('gid');
		}
		
//		2.根据gid查找商品属性
		if($gids){
			$gattr = $this->getAttr($gids);
		}else{
			$gattr = array();
		}
		
		$length = count($gattr);
		$temp = array();
		for ($i=0; $i < $length; $i++) { 
			for ($j=$i+1; $j < $length; $j++) { 
				if($gattr[$i]['name']==$gattr[$j]['name']){
					foreach ($gattr[$j]['value'] as $v) {
						$gattr[$i]['value'][]=$v;
					}
					$temp[] = $j;
				}
			}
		}
		$temp = array_unique($temp);
		foreach ($temp as $k => $v) {
			unset($gattr[$v]);
		}
//		对键名重置序列化
		sort($gattr);
		View::with('gattr',$gattr);
//		3.处理筛选参数param
		$num = count($gattr);
//		判断是否存在提交过来的值，如果没有则全部为0
		if($num>0){
			$param = isset($_GET['param'])?explode('_', $_GET['param']) : array_fill(0, $num, 0);
			View::with('param',$param);
	//		4.根据param的值进行筛选gid
			$regids = $this->filter($param,$gids);
		}else{
			$regids = null;
		}
		
		if($regids){
			//		统计总数
			$total = $this->goodsModel->where("gid IN(".implode(',', $regids).")")->count();
			View::with('all',$total);
	//		分页
			$page = Page::row(20)->desc(['pre'=>'<<<上一页', 'next'=>'下一页>>>'])->make($total);
			
			View::with('page',$page);
	//		获得所有商品数据
			$goodsData = $this->goodsModel->limit(Page::limit())->where("gid IN(".implode(',', $regids).")")->get();
//			获得商品品牌数据
			$bids = $this->goodsModel->where("gid IN(".implode(',', $regids).")")->lists('brand_bid');
//			去除重复的商品bid
			$bids = array_unique($bids);
//			获得品牌信息
			$brandData = $this->brandModel->where("bid IN(".implode(',', $bids).")")->get();
		}else{
			View::with('all',0);
			View::with('page',null);
			View::with('pageTotal',0);
			$goodsData = array();
			$brandData = array();
		}
		
		View::with('brandData',$brandData);
		View::with('goodsData',$goodsData);
		
		
    	View::make();
    }

//	获得商品属性
	private function getAttr($gids){
		$gattr = $this->gattrModel->where("goods_gid in (".implode(',', $gids).")")->groupBy('gavalue')->get();
//		把相同类型id归为一体
		$sameattr = array();
		foreach ($gattr as $k => $v) {
			$sameattr[$v['attribute_atid']][]=$v;
		}
		$temp = array();
//		组合需要的属性名称的数组
		foreach ($sameattr as $k => $v) {
			$temp[]=array(
				'name' => $this->attrModel->where("atid = {$k}")->pluck('atname'),
				'value' => $v
			);
		}
		return $temp;
	}
//	对筛选条件进行处理
	private function filter($param,$cidGids){
		$gids = array();
		foreach ($param as $k => $v) {
			if($v){
//				1.根据gaid查找属性值
				$gavalue = $this->gattrModel->where("gaid = {$v}")->pluck('gavalue');
//				2.根据属性值找到对应的商品id
				$gids[] = $this->gattrModel->where("gavalue='{$gavalue}'")->lists('goods_gid');
			}
		}
//		如果有筛选值
		if($gids){
			$temp = $gids[0];
//			循环两两取交集
			foreach ($gids as $k => $v) {
//				取各个筛选条件的交集
				$temp = array_intersect($temp, $v);
			}
//			在于该分类条件下的gid进行取交集，保证是在该分类下
			return array_intersect($temp, $cidGids);
		}else{//没有筛选值
			return $cidGids;
		}
	}




}
